package FillMythMetadataFilenameParser;
use strict;

sub extract_year_released {
	# an array to hold the found years
	my @foundYears;
	my @possibleYears;
	my $possibleYear;

	my $filename = $_[0];

	# Get the local year
	(my $sec,my $min,my $hour,my $mday,my $mon,my $thisYear,my $wday,my $yday,my $isdst) = localtime(time);
	$thisYear += 1900;

	@possibleYears = $filename =~ /[\[,\(,\{]\d{4}[\],\),\}]/g;

	foreach $possibleYear (@possibleYears) 
	{
		# strip the delimeter characters from the string
		$possibleYear =~ s/[\[,\(,\{,\],\),\}]//g;

		# if the digits are > 1900 and <= the current year its safe to assume its a year
		if (($possibleYear >= 1900) && ($possibleYear <= $thisYear)) {
				push(@foundYears, $possibleYear);
		}
	}

	# if we have a number of years in the array then we will use the one at the end
	if (scalar(@foundYears) > 1) {
        	return $foundYears[scalar(@foundYears)-1];
	}
	#print "COUNT: " . scalar(@foundYears) . "\n\n\n";
	if ((scalar(@foundYears) eq 0) || (!$foundYears[0]))
	{
		return "";
	}
	else
	{
		return $foundYears[0];
	}
}

sub format_episode_title
{
	my $filename = $_[0];
	my $episode = $_[1];
	my $episode_name = $_[2];
	my $show = $_[3];
	my $season = $_[4];
	my $tv_show_title_format = $_[5];
	
	my($episode_with_zero,$season_with_zero);

	if (length $episode eq 1) {$episode_with_zero = "0".$episode;}
	else{$episode_with_zero = $episode;}

	if (length $season eq 1) {$season_with_zero = "0".$season;}
	else{$season_with_zero = $season;}


	if ($tv_show_title_format eq 0)
	{
		return $filename;
	}
	elsif ($tv_show_title_format eq 2)
	{
		return $show." S".$season_with_zero."E".$episode_with_zero." ".$episode_name;
	}
	elsif ($tv_show_title_format eq 3)
	{
		return $show." ".$season."x".$episode_with_zero." ".$episode_name;
	}
	#default
	return $episode_with_zero.": ".$episode_name;
}

sub format_movie_title
{
	my $filename = $_[0];
	my $movie_name = $_[1];
	my $movie_title_format = $_[2];
	
	if ($movie_title_format eq 0)
	{
		return $filename;
	}
	
	#default
	return $movie_name;
}

#return ($show, $season, $episode, $is_tv_show)
sub parse_tv_show_filename
{
	my $filename = $_[0];
	
	my $show;
	my $season;
	my $episode;
	my $is_tv_show = 0;
	#no nfo, and the parsing will get a new show ID
	my $series_id = "";#resetting, so it will be searched.
	#scrubs S04E22.tvrip.avi
	if ($filename =~ m/^(.+)S(\d{1,2})E(\d{1,2})\D.*$/i)
	{
		$show = $1;
		$season = $2;
		$episode = $3;
		print "TV format 1: Show: '$show', S:'$season', E:'$episode'\n";
		$is_tv_show = 1;
	}
	#scrubs.4x22.tvrip.avi
	elsif ($filename =~ m/^(.+)\W+(\d{1,2})x(\d{1,2})\D.*$/i)
	{
		$show = $1;
		$season = $2;
		$episode = $3;
		print "TV format 2: Show: '$show', S:'$season', E:'$episode'\n";
		$is_tv_show = 1;
	}
	#should support the following:
	#scrubs.422.tvrip.avi
	#My Name is Earl 422 - Pinky.720p.h264.tvrip.mkv
	#The Simpsons 2015 - Wedding For Disaster.720p.h264.tvrip.mkv
	#Big Bang Theory, The 301 - The Electric Can Opener Fluctuation.720p.h264.tvrip.avi
	#will ignore (2000) as movie year and ?,000 for 10,000 BC
	#will ignore (Dates 2009-01-01) Series - 2009-01-01
	elsif (!($filename =~ /[\[,\,\{]\d{4}[\],\,\}]/g) 
			&& !($filename =~ m/^.+\d{1,2},\d{3}.*$/i)
			&& !($filename =~ m/^.+\d{2,4}-\d{1,2}-\d{1,2}.*$/i)
			&& ($filename =~ m/^(.*?)\D(\d{1,2})(\d{2})\D.*$/i))#This must be the last one, since we are in search for its tokens.
	{
		$show = $1;
		$season = $2;
		$episode = $3;
		print "TV format 3: Show: '$show', S:'$season', E:'$episode'\n";
		$is_tv_show = 1;
	}
	else
	{
		print "Can not parse '$filename' as tv show. Will try as a movie...\n";
		$is_tv_show = 0;
	}
	
	return ($show, $season, $episode, $is_tv_show);
}

1;
